package com.open.party.cloud.view.freePhoto

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import cn.android.x.model.data.ResultInfo
import com.jiangyy.easydialog.CommonDialog
import com.open.party.cloud.R
import com.open.party.cloud.model.*
import com.open.party.cloud.view.freePhoto.adpater.ImageSelectedShowAdapter
import com.open.party.cloud.viewModel.FreePhotosViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.image.selected.ImgSelectEntity
import com.sinothk.image.selector.PhotoPreviewActivity
import kotlinx.android.synthetic.main.free_photo_add_doing.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * <pre>
 *  创建:  梁玉涛 2019/3/24 on 14:27
 *  项目:
 *  描述:
 *  更新:
 * <pre>
 */
open class FreePhotosAddDoingActivity : PhotosCreatorBaseActivity() {
    var viewModel: FreePhotosViewModel? = null

    override fun getLayoutResId(): Int = R.layout.free_photo_add_doing

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("随手拍处理", "提交") {
            submit()
        }

        bizId = intent.getStringExtra("id") as String

        viewModel = FreePhotosViewModel()

        setImg()
    }

    private fun submit() {
        if (!XUtils.net().isConnected) {
            XUtils.toast().show(R.string.net_error)
            return
        }

        val describeStr: String = describeTv.text.toString()
        if (XUtils.string().isEmpty(describeStr)) {
            XUtils.toast().show("请输入描述内容")
            return
        }

        if (imgSelectList.size == 0) {
            XUtils.toast().show("请上传文件")
            return
        }

        val freePhotosVo = FreePhotosVo()
        freePhotosVo.id = bizId
        // 描述
        freePhotosVo.doneDesc = describeStr
        // 处理图片
        val fileInfoList = ArrayList<FreePhotosFileBean>()
        for (imgBean in imgSelectList) {
            fileInfoList.add(FreePhotosFileBean(imgBean.url, DictionaryBean(" CL", "处理")))
        }
        freePhotosVo.fileList = fileInfoList

        showLoadingDialog("正在提交")
        viewModel?.submitDealWithFreePhotos(freePhotosVo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusSubmitCallback(result: ResultInfo<FreePhotosBean>) {
        if (result.eventType != "submitDealWithFreePhotos") {
            return
        }
        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("提交处理成功")
                finish()
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private var mAlbumSelectedShowAdapter: ImageSelectedShowAdapter? = null

    private fun setImg() {
        mAlbumSelectedShowAdapter = ImageSelectedShowAdapter(this, 8)
        mRvAlbumSelected.layoutManager = GridLayoutManager(this, 4)
        mRvAlbumSelected.setHasFixedSize(true)
        mRvAlbumSelected.adapter = mAlbumSelectedShowAdapter

        mAlbumSelectedShowAdapter!!.setOnItemClickListener { _, position ->
            if (imgSelectList.size == position) {
                openSysCamera(BizType.FREE_PHOTO_IMG)
            } else {
                //图片展示界面
                val imgList: ArrayList<String> = ArrayList()
                for (imgEntity: ImgSelectEntity in imgSelectList) {
                    imgList.add(imgEntity.filePath)
                }
                PhotoPreviewActivity.start(this@FreePhotosAddDoingActivity, position, imgList)
            }
        }

        mAlbumSelectedShowAdapter!!.setItemDelClick { position ->
            dialog(position)
        }
    }

    override fun refreshImgView() {
        mAlbumSelectedShowAdapter?.setData(imgSelectList)
    }

    /*
     * Dialog对话框提示用户删除操作
     * position为删除图片位置
     */
    private fun dialog(position: Int) {
        CommonDialog.Builder(this)
            .setTitle("提示")
            .setMessage("确认移除已添加的图片吗？")
            .setPositiveButton("确认") {
                if (mAlbumSelectedShowAdapter!!.itemCount > 0) {
                    delPosition = position
                    // 删除服务器文件
                    delFileById(imgSelectList[position].id)
                }
            }.setNegativeButton("取消") {

            }.show()
    }

    private var delPosition: Int = 0

    override fun delFileSuccess() {
        // 移除展示数据
        imgSelectList.removeAt(delPosition)
        // 刷新界面
        mAlbumSelectedShowAdapter?.setData(imgSelectList)
        mAlbumSelectedShowAdapter?.notifyDataSetChanged()
    }
}